package org.jeecg.modules.CheckTask.service.impl;


import org.jeecg.modules.CheckTask.entity.CheckTask;
import org.jeecg.modules.CheckTask.entity.CheckTaskDetail;
import org.jeecg.modules.CheckTask.mapper.CheckTaskDetailMapper;
import org.jeecg.modules.CheckTask.mapper.CheckTaskMapper;
import org.jeecg.modules.CheckTask.service.ICheckTaskService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 排查任务表
 * @Author: jeecg-boot
 * @Date:   2023-02-21
 * @Version: V1.0
 */
@Service
public class CheckTaskServiceImpl extends ServiceImpl<CheckTaskMapper, CheckTask> implements ICheckTaskService {

	@Autowired
	private CheckTaskMapper checkTaskMapper;
	@Autowired
	private CheckTaskDetailMapper checkTaskDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CheckTask checkTask, List<CheckTaskDetail> checkTaskDetailList) {
		checkTaskMapper.insert(checkTask);
		if(checkTaskDetailList!=null && checkTaskDetailList.size()>0) {
			for(CheckTaskDetail entity:checkTaskDetailList) {
				//外键设置
				entity.setTaskId(checkTask.getId());
				checkTaskDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CheckTask checkTask,List<CheckTaskDetail> checkTaskDetailList) {
		checkTaskMapper.updateById(checkTask);
		
		//1.先删除子表数据
		checkTaskDetailMapper.deleteByMainId(checkTask.getId());
		
		//2.子表数据重新插入
		if(checkTaskDetailList!=null && checkTaskDetailList.size()>0) {
			for(CheckTaskDetail entity:checkTaskDetailList) {
				//外键设置
				entity.setTaskId(checkTask.getId());
				checkTaskDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		checkTaskDetailMapper.deleteByMainId(id);
		checkTaskMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			checkTaskDetailMapper.deleteByMainId(id.toString());
			checkTaskMapper.deleteById(id);
		}
	}
	
}
